#!/usr/bin/perl
require "pdcusrgroup-lib.pl";

&ReadParse();

my $out, $i, $error_msg, $global, $security;

#&header($text{'index_title'}, "", undef, 0, 1);
#`$config{'winbind_restart'} 2>&1`;
#sleep(1);

&foreign_require("samba", "samba-lib.pl");
$global = &foreign_call("samba", "get_share", "global");
$security = &foreign_call("samba", "getval", "security");

if (!($security =~ /^ADS/))
{
	$error_msg = $text{"error_msg_refresh_nodomain"};
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'pdcusrgroup_naslog_enent_refresh_nodomain'};
	$formatstr = $text{'pdcusrgroup_naslog_refresh_nodomain'};
	goto ERROR_OUT;
}

$out = `$config{'winbind_status'} 2>/dev/null`;
#print "<p>", $out;
$i = 0;

while(($out =~ /stopped/) && ($i < 2))
{
  $out = `$config{'winbind_start'} 2>/dev/null`;
  sleep(3);
  $out = `$config{'winbind_status'} 2>/dev/null`;
  #print "<p>", $out;
  $i++;
}

`$config{'winbind_info_user'} > $config{'pdcuserfile'} 2>/dev/null`;
`$config{'winbind_info_group'} > $config{'pdcgroupfile'} 2>/dev/null`;
$error_msg = &checkpdcfile();

if($error_msg)
{
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'pdcusrgroup_naslog_enent_verify_pass_fail'};
	$formatstr = $text{'pdcusrgroup_naslog_verify_pass_fail'};
	goto ERROR_OUT;
}

$logminor= $text{'module_name'};
$logpriority=$text{'naslog_info'};
$event = $text{'pdcusrgroup_naslog_enent_refresh_success'};
$formatstr = $text{'pdcusrgroup_naslog_refresh_success'};
&nas_syslog($logminor, $logpriority, $event, $formatstr);
&redirect("index.cgi");

ERROR_OUT:

&nas_syslog($logminor, $logpriority, $event, $formatstr);
&error_setup($text{"error_title_verify_password"});
&error($error_msg);
#&footer("/", $text{'index'});
